package shoppingmall.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.manage.mapper.CommodityStandardMapper;
import shoppingmall.manage.pojo.CommodityStandard;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.service.CommodityStandardService;

import java.util.List;

@Service
public class CommodityStandardServiceImpl implements CommodityStandardService {

    @Autowired
    private CommodityStandardMapper commodityStandardMapper;

    /**
     * 查询所有商品规格属性，分页(功能目前没做)
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean getCommodityStandardList(int page, int pageSize) {

        //调用commodityStandardMapper的相关方法分页查询商品规格
        int start = (page - 1) * pageSize;
        List<CommodityStandard> commodityStandardList = commodityStandardMapper.getCommodityStandardList(start, pageSize);
        //查询商品规格总记录条数
        int count = commodityStandardMapper.getCommodityStandardCount();
        //返回数据
        return new PageBean(count, commodityStandardList);
    }

    /**
     * 查询某个商品下的规格集合(commodityId = ?)
     * @param commodityId
     * @return
     */
    @Override
    public PageBean getCommodityStandardListByCommodityId(int commodityId) {
        //某个商品下的规格列表
        List<CommodityStandard> commodityStandardList = commodityStandardMapper.getCommodityStandardListByCommodityId(commodityId);
        //规格数据条数
        int count = commodityStandardMapper.getCommodityStandardCountByCommodityId(commodityId);
        return new PageBean(count,commodityStandardList);
    }

    /**
     * 添加一条商品规格
     * @param commodityStandard
     * @return
     */
    @Override
    public boolean saveCommodityStandard(CommodityStandard commodityStandard) {
        //调用mapper层插入一条规格数据
        try {
            commodityStandardMapper.saveCommodityStandard(commodityStandard);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 修改一条商品规格
     * @param commodityStandard
     * @return
     */
    @Override
    public boolean updateCommodityStandard(CommodityStandard commodityStandard) {
        //调用mapper层修改一条数据
        try {
            commodityStandardMapper.updateCommodityStandard(commodityStandard);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除一条商品规格
     * @param commodityStandardId
     * @return
     */
    @Override
    public boolean deleteCommodityStandard(int commodityStandardId) {
        //调用mapper层删除一条商品规格
        try {
            commodityStandardMapper.deleteCommodityStandard(commodityStandardId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
